﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AutoBackupDatabase.DBConection;
using System.Data;
using System.Data.SqlClient;
using AutoBackupDatabase.ENT;

namespace AutoBackupDatabase.DAL
{
   public class UNITDAL
    {
       private DbConnection conn;
       public UNITDAL()
       {
           conn = new DbConnection();
       }

       public List<UNITENT> GET_UNIT_ALL()
       {
           string query = string.Format("exec GET_UNIT_ALL ");
           DataTable ds = conn.executeGetQuery(query);
           List<UNITENT> List = new List<UNITENT>();
           foreach (DataRow item in ds.Rows)
           {
               UNITENT UNITent = new UNITENT();
               UNITent.UNITID = int.Parse( item["UNITID"].ToString());
               UNITent.UNITNAME = item["UNITNAME"].ToString();
               List.Add(UNITent);
           }

           return List;
       }
       public bool INSERTUNIT(UNITENT ent)
       {
           string query = "exec  INSERTUNIT @UNITNAME";
           SqlParameter[] sqlParameters = new SqlParameter[1];
          
           sqlParameters[0] = new SqlParameter("@UNITNAME", SqlDbType.NVarChar);
           sqlParameters[0].Value = Convert.ToString(ent.UNITNAME);
           return conn.executeInsertQuery(query, sqlParameters);
       }
       public bool UPDATEUNITBYUNITID(UNITENT ent)
       {
           string query = "exec  UPDATEUNITBYUNITID @UNITID,@UNITNAME";
           SqlParameter[] sqlParameters = new SqlParameter[2];
           sqlParameters[0] = new SqlParameter("@UNITID", SqlDbType.Int);
           sqlParameters[0].Value = Convert.ToString(ent.UNITID);
           sqlParameters[1] = new SqlParameter("@UNITNAME", SqlDbType.NVarChar);
           sqlParameters[1].Value = Convert.ToString(ent.UNITNAME);
           return conn.executeUpdateQuery(query, sqlParameters);
       }
       public bool DELTELEUNITBYUNITID(UNITENT ent)
       {
           string query = "exec  DELTELEUNITBYUNITID @UNITID";
           SqlParameter[] sqlParameters = new SqlParameter[1];
           sqlParameters[0] = new SqlParameter("@UNITID", SqlDbType.Int);
           sqlParameters[0].Value = Convert.ToString(ent.UNITID);
           return conn.executeDeleteQuery(query, sqlParameters);
       }
    }
}
